﻿package me.idashu.attg.webService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import me.idashu.attg.domain.AdverEntity;
import me.idashu.attg.domain.AttenEntity;
import me.idashu.attg.domain.GoodsEntity;
import me.idashu.attg.domain.LabelEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.domain.ParamterEntity;
import me.idashu.attg.domain.ShopEntity;
import me.idashu.attg.domain.UserEntity;
import me.idashu.attg.service.AdverService;
import me.idashu.attg.service.AttenService;
import me.idashu.attg.service.GoodesService;
import me.idashu.attg.service.LabelSerivce;
import me.idashu.attg.service.ShopService;
import me.idashu.attg.service.UserService;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AllWebSerivce {

	@Autowired
	private UserService userService;

	@Autowired
	private ShopService shopService;
	
	@Autowired
	private GoodesService goodesService;

	@Autowired
	private AttenService attenService;
	
	@Autowired
	private LabelSerivce labelSerivce;
	
	@Autowired
	private AdverService adverService;

	@RequestMapping(value = "/user/register", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject register(@ModelAttribute UserEntity userEntity) {
		JSONObject json = new JSONObject();
		try {
			UserEntity user = userService.findUserByUserName(userEntity);
			if (user != null) {
				json.put("status", 400);
				json.put("message", "用户名已存在！");
			} else {
				userService.registerUser(userEntity);
				json.put("status", 200);
				json.put("message", "注册成功！");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/user/login", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject login(@ModelAttribute UserEntity userEntity) {
		JSONObject json = new JSONObject();
		try {
			UserEntity user = userService
					.findUserByUserNameAndPassWord(userEntity);
			if (user != null) {
				json.put("status", 200);
				json.put("message", "登录成功");
			} else {
				json.put("status", 400);
				json.put("message", "用户名和密码错误！");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findAllShops", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject findAllShops(@ModelAttribute PageEntity pageEntity) {
		JSONObject json = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ShopEntity shopEntity = new ShopEntity();
			int total = shopService.findCountShopsByPage(shopEntity);
			pageEntity.setTotalPage(total);
			pageEntity.setPageLess(pageEntity.getPage());
			map.put("pageEntity", pageEntity);
			map.put("shopEntity", shopEntity);
			List<ShopEntity> shops = shopService.findShopsByPage(map);
			json.put("status", 200);
			json.put("shops", shops);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/searchShops", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchShops(@ModelAttribute ShopEntity shopEntity) {
		JSONObject json = new JSONObject();
		try {
			List<ShopEntity> shops = shopService.searchShops(shopEntity);
			json.put("status", 200);
			json.put("shops", shops);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/insertUserAtten", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertUserAtten(@ModelAttribute AttenEntity attenEntity) {
		JSONObject json = new JSONObject();
		try {
			attenEntity.setTime(new Date());
			attenService.insertAtten(attenEntity);
			json.put("status", 200);
			json.put("message", "添加关注成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findAttenShops", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject findAttenShops(@RequestParam("userid") long userid) {
		JSONObject json = new JSONObject();
		try {
			List<ShopEntity> shops = shopService.findShopsByAttens(userid);
			json.put("status", 200);
			json.put("shops", shops);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findOneShop", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findOneShop(@RequestParam("id") long id) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			ShopEntity shop = shopService.findShopById(id);
			List<GoodsEntity> goodses = goodesService.findGoodesByShopId(shop.getId());
			json.put("status", 200);
			json.put("shop", shop);
			json.put("goodses", goodses);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}
	
	@RequestMapping(value = "/findOneShopDescption", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findOneShopDescption(@RequestParam("id") long id) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			ShopEntity shop = shopService.findShopById(id);
			List<AdverEntity> advers = adverService.findAdversByShopIdToInter(shop.getId());
			json.put("status", 200);
			json.put("description", shop.getContent());
			json.put("advers", advers);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findAllGoods", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject findAllGoods(@ModelAttribute PageEntity pageEntity,
			@ModelAttribute GoodsEntity goodsEntity,
			@ModelAttribute ParamterEntity paramterEntity) {
		JSONObject json = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("paramterEntity", paramterEntity);
			map.put("goodsEntity", goodsEntity);
			int total = goodesService.findCountAllGoodsByPage(map);
			pageEntity.setTotalPage(total);
			pageEntity.setPageLess(pageEntity.getPage());
			map.put("pageEntity", pageEntity);
			List<GoodsEntity> goods = goodesService.findAllGoodsByPage(map);
			json.put("status", 200);
			json.put("goods", goods);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}
	
	@RequestMapping(value = "/findAllLabels", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject findAllLabels(@ModelAttribute LabelEntity labelEntity) {
		JSONObject json = new JSONObject();
		try {
			List<LabelEntity> labels = labelSerivce.findAllLabelByType(labelEntity);
			json.put("status", 200);
			json.put("labels", labels);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}
	
	@RequestMapping(value = "/findOneGoods", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject findOneGoods(@RequestParam("id") long id) {
		JSONObject json = new JSONObject();
		try {
			GoodsEntity good = goodesService.findGoodById(id);
			json.put("status", 200);
			json.put("good", good);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}
	
	@RequestMapping(value = "/searchGoods", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchGoods(@ModelAttribute GoodsEntity goodsEntity) {
		JSONObject json = new JSONObject();
		try {
			List<GoodsEntity> goods = goodesService.searchGoodsEntity(goodsEntity);
			json.put("status", 200);
			json.put("goods", goods);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}
	
	@RequestMapping(value = "/findOneAdver", method = RequestMethod.GET)
	@ResponseBody
	public void getUserNewsHtml(@ModelAttribute AdverEntity adverEntity,
			HttpServletResponse response) {
		try {
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			AdverEntity adver = adverService.findOne(adverEntity.getId());
			out.println(adver.getPosdesc());
		} catch (Exception ex) {
			try {
				PrintWriter out = response.getWriter();
				response.setContentType("text/html");
				out.println("error!");
			} catch (IOException e) {
				e.printStackTrace();
			}
			ex.printStackTrace();
		}
	}
	

}
